//
//  SimpleMessageCell.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/8/10.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "SimpleMessageCell.h"
#import "SimpleMessage.h"

@interface SimpleMessageCell ()

-(void)initialize;

@end
@implementation SimpleMessageCell

-(NSDictionary *)attributeDictionary{
    if (self.messageDirection == MessageDirection_SEND) {
        return @{
                 @(NSTextCheckingTypeLink) : @{NSForegroundColorAttributeName : [UIColor blueColor]},
                 @(NSTextCheckingTypePhoneNumber) : @{NSForegroundColorAttributeName : [UIColor blueColor]}
                 };
    } else {
        return @{
                 @(NSTextCheckingTypeLink) : @{NSForegroundColorAttributeName : [UIColor blueColor]},
                 @(NSTextCheckingTypePhoneNumber) : @{NSForegroundColorAttributeName : [UIColor blueColor]}
                 };
    }
    return nil;
}
-(NSDictionary *)highlightedAttributeDictionary{
    return [self attributeDictionary];

}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];

    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}
-(void)initialize{
    
    self.textLabel = [[RCAttributedLabel alloc]init];
    self.textLabel.center = CGPointMake(self.messageTimeLabel.center.x+365, self.baseContentView.center.y+50);
    self.textLabel.bounds = CGRectMake(0, 0, 400, 50);
    self.textLabel.attributeDictionary = [self attributeDictionary];
    self.textLabel.highlightedAttributeDictionary = [self highlightedAttributeDictionary];
    [self.textLabel setFont:[UIFont systemFontOfSize:Text_Message_Font_Size]];
    self.textLabel.numberOfLines = 0;
    [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.textLabel setTextAlignment:NSTextAlignmentCenter];
    [self.textLabel setTextColor:[UIColor blackColor]];
    self.textLabel.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    UIButton *btn = [[UIButton alloc]initWithFrame:self.textLabel.frame];
    [btn addTarget:self action:@selector(clickLookInfoBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.baseContentView addSubview:btn];
    [self.baseContentView addSubview:self.textLabel];
}
+(CGSize)sizeForMessageModel:(RCMessageModel *)model withCollectionViewWidth:(CGFloat)collectionViewWidth referenceExtraHeight:(CGFloat)extraHeight{
    return     CGSizeMake(400, 80);

}
//当应用自定义消息时，必须实现该方法来返回cell的Size
-(void)setDataModel:(RCMessageModel *)model{
    [super setDataModel:model];
    
    [self setAutoLayout];
}
-(void)setAutoLayout{
    
    if ([self.model.content isMemberOfClass:[SimpleMessage class]]){
        
        SimpleMessage *_textMessage = (SimpleMessage *)self.model.content;

        NSString *str = [NSString stringWithFormat:@"您已于%@结束辅导    查看学生反馈",_textMessage.content];
        NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:str];
        //找出特定字符在整个字符串中的位置
        NSRange redRange = NSMakeRange([[contentStr string] rangeOfString:@"查看学生反馈"].location, [[contentStr string] rangeOfString:@"查看学生反馈"].length);
        //修改特定字符的颜色
        [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#20B097"] range:redRange];
        //修改特定字符的字体大小
        [contentStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:redRange];
        [self.textLabel setAttributedText:contentStr];
        
    }
}

-(void)longPressed:(id)sender{
    UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *)sender;
    if (press.state == UIGestureRecognizerStateEnded) {
        //DebugLog(@”long press end”);
        return;
    } else if (press.state == UIGestureRecognizerStateBegan) {
        [self.delegate didLongTouchMessageCell:self.model inView:self.bubbleBackgroundView];
    }
}
-(void)clickLookInfoBtn{
    if (self.lookInfoBtnClick) {
        self.lookInfoBtnClick();
    }
}
@end
